<?php
class PaymentModule{
	function test(){
		global $_G;
		if(!ISADMIN)exit;
		$amount = isset($_GET['amount'])?$_GET['amount']:0.01;
		$orderid = isset($_GET['orderid'])?$_GET['orderid']:0;
		$type = isset($_GET['ptype'])?$_GET['ptype']:'alipay';
				$txn = array(
					'uid' => $_G['uid'],
					'type' => 'testpay',
					'mail' => $_G['users']['email'],
					'payment_type' => $type,
					'gross'	=> 0.01,
					'created'=>TIMESTAMP,
				);
				$txn['txnid']=$_G['db']->insert("orders",$txn);
		
		$this->payurl($txn,$type);
	}
	function payurl($txn,$type='alipay') {
		include libfile('payment/'.$type.'.php');
		switch($type) {
			case 'tenpay':
				$payment = new TenpayModule;
				$return = $payment->payurl($txn);
				header("Location: $return");
				break;
			case 'quickpay':
				$payment = new QuickpayModule;
				$return = $payment->payurl($txn);
				echo $return.'<script type="text/javascript" reload="1">document.getElementById("payform").submit();</script>';
				break;	
			case 'alipay':
				$payment = new AlipayModule;
				$return = $payment->payurl($txn);
				header("Location: $return");
				break;
		}
		exit;
	}
	function pay(){
		global $_G;
		$id=getGP('id','G','int');
		$lang = lang::load('point');
		if(!$id){
			exit('<p align="center">'.$lang['payment_error'].'</p><p align="center"><a href="'.url('home.php?m=payment&f=pay&ajax=1&id='.$id).'">'.$lang['turn_back'].'</a> <a href="javascript:;" onclick="window.parent.location.reload();">'.$lang['close'].'</a> </p>');
		}
		$txn= $_G['db']->fetch('orders','*',array('txnid'=>$id,'uid'=>$_G['uid'],'status'=>0));
		if(!$txn['txnid']){
			exit('<p align="center">'.$lang['payment_error'].'</p><p align="center"><a href="'.url('home.php?m=payment&f=pay&ajax=1&id='.$id).'">'.$lang['turn_back'].'</a> <a href="javascript:;" onclick="window.parent.location.reload();">'.$lang['close'].'</a> </p>');
		}
		
		if(!IS_POST){
			foreach($_G['settings']['payment'] as $k=>$v){
				$payments[$k]=unserialize(decrypt($v));
			}
			include template('uc/payment_ajax');
			exit;
		}else{
			$paytype = getGP('payment','P');
			if(!$paytype){
				exit('<p align="center">'.$lang['payment_error'].'</p><p align="center"><a href="'.url('home.php?m=payment&f=pay&ajax=1&id='.$id).'">'.$lang['turn_back'].'</a> <a href="javascript:;" onclick="window.parent.location.reload();">'.$lang['close'].'</a> </p>');
			}
			$_G['db']->update('orders',array('payment_type'=>$paytype),array('txnid'=>$id));
			
			if($txn['status']>0){
				exit('<p align="center">'.$lang['had_payment'].'</p><p align="center"><a href="'.url('home.php?m=payment&f=pay&ajax=1&id='.$id).'">'.$lang['turn_back'].'</a> <a href="javascript:;" onclick="window.parent.location.reload();">'.$lang['close'].'</a> </p>');
			}
			$this->payurl($txn,$paytype);
		}
	}
	function notify() {
		include libfile('payment/'.$_REQUEST['type'].'.php');
		$classname = ucfirst(strtolower($_REQUEST['type'])).'Module';
		$payment = new $classname;
		$return = $payment->notify();
		switch ( $classname ) {
			case 'AlipayModule':
				if($return['check']) {//通过验证
					$txn = $_G['db']->fetch('orders','*',array('txnid'=>$return['txnid']));
					if($txn['payment_date']<1 && floatval($txn['gross'])==floatval($return['price']) && $return['status']){
						$_G['db']->query("UPDATE".table('orders')."SET trade_no='".$return['trade_no']."',payment_date=".TIMESTAMP." WHERE txnid='".$txn['txnid']."'");
						if($txn['type']=='pointpay'){
							$this->updatepoint($txn,true);
						}
						exit("success");
					}
				}
				exit("fail");
				break;
			case 'TenpayModule':
				if($return['check']){
					$txn = $_G['db']->fetch('orders','*',array('txnid'=>$return['txnid']));
					if($return['status'] && $txn['payment_date']<1 && floatval($txn['gross'])==floatval($return['price'])){
						$_G['db']->query("UPDATE".table('orders')."SET trade_no='".$return['trade_no']."',payment_date=".TIMESTAMP." WHERE txnid='".$txn['txnid']."'");
						if($txn['type']=='pointpay'){
							$this->updatepoint($txn,true);
						}
						exit("success");
					}
				}
				exit("fail");
				break;	
			case 'QuickpayModule':
				if($return['check']){
					$txn = $_G['db']->fetch('orders','*',array('trade_no'=>$return['txnid']));
					if($return['status'] && $txn['payment_date']<1 && floatval($txn['gross'])==floatval($return['price'])){
						$_G['db']->query("UPDATE".table('orders')."SET trade_no='".$return['trade_no']."',payment_date=".TIMESTAMP." WHERE txnid='".$txn['txnid']."'");
						if($txn['type']=='pointpay'){
							$this->updatepoint($txn,true);
						}
						exit("success");
					}
				}
				exit("fail");
				break;
		}
		
	}
	
	function result(){
		include libfile('payment/'.$_REQUEST['type'].'.php');
		$classname = ucfirst(strtolower($_REQUEST['type'])).'Module';
		
		switch ( $classname ) {
			case 'AlipayModule':
				$txnid = $_GET['out_trade_no'];
				$txn = $_G['db']->fetch('orders','*',array('txnid'=>$return['txnid']));
				if($txn['payment_date']>0){
						msg('支付成功');
					}else{
						msg('支付失败');
					}
				break;
			case 'TenpayModule':
				$resHandler = new ResponseHandler();
				$resHandler->setKey($key);
				if($resHandler->isTenpaySign()) {
					$txnid=$resHandler->getParameter("out_trade_no");
					$txn = $_G['db']->fetch('orders','status',array('txnid'=>$return['txnid']));
					if($txn['payment_date']>0){
						msg('支付成功');
					}else{
						msg('支付失败');
					}
				}
				break;
			case 'QuickpayModule':
				$payment = new $classname;
				$return = $payment->notify();
				if($return['check']){
					$txn = $_G['db']->fetch('orders','*',array('trade_no'=>$return['txnid']));
					if($return['status'] && $txn['payment_date']<1 && floatval($txn['gross'])==floatval($return['price'])){
						$_G['db']->query("UPDATE".table('orders')."SET status='1',trade_no='".$return['trade_no']."',payment_date=".TIMESTAMP." WHERE txnid='".$txn['txnid']."'");
						$txn['payment_date']=TIMESTAMP;
					}
					if($txn['payment_date']>0){
						msg('支付成功');
					}
				}
				msg('支付失败');
				break;
		}
	}
	
	function updatepoint($txn=array(),$flag=false){
		if(!$flag){
			return '';
		}
		point('pointbuy',$txn);
	}
	
}